package com.snailxr.base.support.handler;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.snailxr.base.support.util.Util;
import com.snailxr.base.task.domain.jx.XZSP;
import com.trs.web2frame.WCMServiceCaller;
import com.trs.web2frame.dispatch.Dispatch;
import com.trs.web2frame.util.JsonHelper;

/**
 * 行政审批数据处理类,单例模式
 *  @author luoshijian
 *
 */
@Component("xzspHandler")
@Scope("singleton")
public class XZSPHandler {
	public final Logger log = Logger.getLogger(this.getClass());
	public XZSP handle(JSONObject item_json){
		XZSP xzsp = new XZSP();
		
		String sxmc="";
		String slfw="-";
		String bljg="-";
		String jdtsdh="-";
		String sltj="";
		String blcx="-";
		String lct="-";
		String blsx="-";
		String sfyj="-";
		String sfbz="-";
		String bgxz="-";
		String wsbl="-";
		String wscx="-";
		String zxdh="-";
		String blcs="-";
		String itemId="";
		String CODE="";
		String ORG_CODE="";
		String blckhdz="-";
		String flyj="";
		String sxcl="-";
		String kbl="-";
		String kzxblsj="-";
		String ztfl="-";
		
		xzsp.setObjectId(0);
		xzsp.setChannelId(1298);
		
		itemId = item_json.getString("ID");
		xzsp.setItemId(itemId);
		
		CODE = item_json.getString("CODE");
		xzsp.setCODE(CODE);
		
		ORG_CODE = item_json.getString("ORG_CODE");
		xzsp.setORG_CODE(ORG_CODE);
		
		sxmc = item_json.getString("NAME");
		xzsp.setSxmc(sxmc);
		
		if(item_json.getString("SERVICE_OBJECT").indexOf("0,1")>-1){
			slfw="个人及法人";
		}else if(item_json.getString("SERVICE_OBJECT").indexOf("0")>-1){
			slfw="个人";
		}else if(item_json.getString("SERVICE_OBJECT").indexOf("1")>-1){
			slfw="法人";
		}
		xzsp.setSlfw(slfw);
		
		bljg = item_json.getString("ORG_NAME");
		xzsp.setBljg(bljg);
		
		JSONArray ckdh_array=JSONArray.parseArray(item_json.getString("venuesList"));
		for (int j = 0; j < ckdh_array.size(); j++) {
			JSONObject ckdh_json=ckdh_array.getJSONObject(j);
			jdtsdh=ckdh_json.getString("COMPLAINT_PHONE");
			zxdh=ckdh_json.getString("PHONE");
			blckhdz=ckdh_json.getString("NAME")+"&nbsp;&nbsp;"+ckdh_json.getString("ADDRESS");
			kzxblsj=ckdh_json.getString("OFFICE_HOUR");
		}
		xzsp.setJdtsdh(jdtsdh);
		xzsp.setZxdh(zxdh);
		xzsp.setBlckhdz(blckhdz);
		xzsp.setKzxblsj(kzxblsj);
		
		JSONArray sltj_array=JSONArray.parseArray(item_json.getString("conditionList"));
		if(sltj_array.size()==1){
			JSONObject sltj_json=sltj_array.getJSONObject(0);
			sltj+="<span class='blcx_con'>"+sltj_json.getString("NAME")+"</span><br/>";
		}else{
			for (int j = 0; j < sltj_array.size(); j++) {
				JSONObject sltj_json=sltj_array.getJSONObject(j);
				sltj+="<span class='sltj_n'>"+(j+1)+"</span><span class='blcx_dot'>.</span><span class='blcx_con'>"+sltj_json.getString("NAME")+"</span><br/>";
			}
		}
		xzsp.setSltj(sltj);
		
		JSONArray blcx_array=JSONArray.parseArray(item_json.getString("processList"));
		for (int j = 0; j < blcx_array.size(); j++) {
			JSONObject blcx_json=blcx_array.getJSONObject(j);
			blcx=blcx_json.getString("CONTENT")+"<br/>";
		}
		xzsp.setBlcx(blcx);
		
		JSONArray lct_array=JSONArray.parseArray(item_json.getString("outProcessList"));
		for (int j = 0; j < lct_array.size(); j++) {
			JSONObject lct_json=lct_array.getJSONObject(j);
			String FILE_NAME=lct_json.getString("FILE_NAME");
			int len=FILE_NAME.length();
			int begin=FILE_NAME.lastIndexOf(".");
			String ext=FILE_NAME.substring(begin+1,len);
//			String filename=FILE_NAME.substring(begin);
//			httpUtil.downloadFromUrl(xzsp.getDownLoadUrl()+lct_json.getString("URL"), xzsp.getFilePath(),ext);//TODO 注释做测试
			if(ext.indexOf("png")>-1||ext.indexOf("jpg")>-1||ext.indexOf("gif")>-1){
				lct+="<img class='lct' src='"+xzsp.getFileUrl()+lct_json.getString("URL")+"."+ext+"'/><br/>";
			}else{
				lct+="<a class='lct' href='"+xzsp.getFileUrl()+lct_json.getString("URL")+"."+ext+"'>办事流程图资料</a><br/>";
			}
		}
		xzsp.setLct(lct);
		
		blsx="法定时限："+item_json.getString("LAW_TIME")+"工作日，承诺时限："+item_json.getString("AGREE_TIME")+"工作日";
		if("2".equals(item_json.getString("ASSORT"))){
			blsx="即办件";
		}
		xzsp.setBlsx(blsx);
		
		String IS_CHARGE = item_json.getString("IS_CHARGE");
		if("1".equals(IS_CHARGE)){
			JSONArray sfyj_array=JSONArray.parseArray(item_json.getString("farelList"));
			for (int j = 0; j < sfyj_array.size(); j++) {
				JSONObject sfyj_json=sfyj_array.getJSONObject(j);
				sfyj+="<span class='sfyj_con'><strong>收费名称：</strong>"+sfyj_json.getString("NAME")+"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>收费依据：</strong>"+sfyj_json.getString("BASIS")+"</span><br/>";
				sfbz+="<span class='sfbz_con'><strong>收费名称：</strong>"+sfyj_json.getString("NAME")+"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>收费标准：</strong>"+sfyj_json.getString("STANDARD")+"</span><br/>";
			}
		}else{
			sfyj="不收费";
			sfbz="不收费";
		}
		xzsp.setSfyj(sfyj);
		xzsp.setSfbz(sfbz);
		
		JSONArray sxcl_array=JSONArray.parseArray(item_json.getString("materialList"));
		sxcl="<table class='table'>"
			 +"<tr>"
				+"<td class='top td-tit' align='center' style='width:300px'>材料名称</td>"
				+"<td class='top td-tit' align='center' style='width:100px'>是否必须</td>"
				+"<td class='top td-tit' align='center' style='width:100px'>原件份数</td>"
				+"<td class='top td-tit' align='center' style='width:100px'>复印件份数</td>"
				+"<td class='top td-tit' align='center' style='width:200px'>示范材料</td>"
				+"<td class='top td-tit' align='center' style='width:200px'>备注</td>"
			  +"</tr>";
		for (int j = 0; j < sxcl_array.size(); j++) {
			String sfwb_url="";
			String sysm="";
			String sxcl_bz="";
			
		    //表格下载信息
			JSONObject bgxz_json=sxcl_array.getJSONObject(j);
			if("Apply".equals(bgxz_json.getString("BUSINESS_TYPE"))){
				String FILE_NAME=bgxz_json.getString("SAMPLE_NAME");
				if(!"".equals(FILE_NAME) && FILE_NAME != null){
					int len=FILE_NAME.length();
					int begin=FILE_NAME.lastIndexOf(".");
					String ext=FILE_NAME.substring(begin+1,len);
//					String filename=FILE_NAME.substring(0,begin);
					//String fileResult=httpUtil.downloadFromUrl(xzsp.getDownLoadUrl()+bgxz_json.getString("SAMPLE"), xzsp.getFilePath(),ext);//TODO
					String fileResult= "Successful";//TODO 写死做测试
					if("Successful".equals(fileResult)){
						bgxz+="<a class='bgxz_a' href='"+xzsp.getFileUrl()+bgxz_json.getString("SAMPLE")+"."+ext.replaceAll(";","")+"'>"+bgxz_json.getString("SAMPLE_NAME")+"</a>"+"<br/>";//表格下载
						sfwb_url="<a class='bgxz_a' href='"+xzsp.getFileUrl()+bgxz_json.getString("SAMPLE")+"."+ext.replaceAll(";","")+"'>"+bgxz_json.getString("SAMPLE_NAME")+"</a>";//所需材料中示范文本链接地址
					}
				}
				
				//所需材料信息
				JSONObject sxcl_json=sxcl_array.getJSONObject(j);
				String must=sxcl_json.getString("MUST");
				if(must.equals("0")){
					must="否";
				}else{
					must="是";
				}
				sysm=sxcl_json.getString("WHAT");
				if("".equals(sysm)||"null".equals(sysm)||sysm==null){
					sysm="";
				}
				sxcl_bz=sxcl_json.getString("REMARK");
				if("".equals(sxcl_bz)||"null".equals(sxcl_bz)||sxcl_bz==null){
					sxcl_bz="";
				}
				sxcl+="<tr>"
					+"<td>"+sxcl_json.getString("NAME")+"</td>"
					+"<td align='center'>"+must+"</td>"
					+"<td align='center'>"+sxcl_json.getString("ORIGIN")+"</td>"
					+"<td align='center'>"+sxcl_json.getString("COPY")+"</td>"
					+"<td>"+sfwb_url+"</td>"
					+"<td>"+sxcl_bz+"</td>"
					+"</tr>";
			}
		}
		sxcl+="</table>";
		xzsp.setBgxz(bgxz);
		xzsp.setSxcl(sxcl);
		
		JSONArray blcs_array=JSONArray.parseArray(item_json.getString("handlingList"));
		for (int j = 0; j < blcs_array.size(); j++) {
			JSONObject blcs_json=blcs_array.getJSONObject(j);
			if(blcs.equals("")){
				blcs=blcs_json.getString("REMARK");//可在线办理次数
			}
			String bl = blcs_json.getString("BL"); //网上办理
			String cx = blcs_json.getString("CX");  //网上查寻
			
			if(!"".equals(bl)&&bl!=null){
				wsbl = bl;
			}
			if(!"".equals(cx)&&cx!=null){
				wscx = cx;
			}
		}
		xzsp.setWsbl(wsbl);
		xzsp.setWscx(wscx);
		xzsp.setBlcs(blcs);
		
		JSONArray flyj_array=JSONArray.parseArray(item_json.getString("doomList"));
		if(flyj_array.size()==1){
			JSONObject flyj_json=flyj_array.getJSONObject(0);
			flyj+="<span class='sfbz_con'><strong>法律名称：</strong>"+flyj_json.getString("LAW_NAME")+"；<br/><strong>条款名称：</strong>"+flyj_json.getString("NAME")+"；<br/><strong>条款内容：</strong>"+flyj_json.getString("CONTENT")+"</span><br/>";
		}else{
			for (int j = 0; j < flyj_array.size(); j++) {
				JSONObject flyj_json=flyj_array.getJSONObject(j);
				flyj+="<span class='flyj_n'>"+(j+1)+"</span><span class='flyj_dot'>.</span><span class='sfbz_con'><strong>法律名称：</strong>"+flyj_json.getString("LAW_NAME")+"；<br/>&nbsp;&nbsp;<strong>条款名称：</strong>"+flyj_json.getString("NAME")+"；<br/>&nbsp;&nbsp;<strong>条款内容：</strong>"+flyj_json.getString("CONTENT")+"</span><br/>";
			}
		}
		xzsp.setFlyj(flyj);
		
		kbl = "1";
		xzsp.setKbl(kbl);
		
		ztfl=item_json.getString("TITLE_NAME");
		xzsp.setZtfl(ztfl);
		
		return xzsp;
	}
	
	/**
	 * 将行政审批数据保存至wcm
	 * @param xzsp
	 */
	public String saveMetaViewData(XZSP xzsp){
		String sServiceId = "wcm6_MetaDataCenter";//服务名
		String sMethodName = "saveMetaViewData";//方法名
		Map<String,Object> oPostData = Util.ConvertObjToMap(xzsp);
		if(oPostData.get("sxmc").toString().indexOf("（并联）")>0){
			return null;
		}
		Dispatch oDispatch = WCMServiceCaller.Call(sServiceId, sMethodName,oPostData, true);//保存到行政审批
		Map<String,Object> ojson = oDispatch.getJson();
		System.out.println("#######################"+ojson.get("METAVIEWDATA"));
		String metaDataid = JsonHelper.getValueAsString(ojson, "METAVIEWDATA.METADATAID");//获取按主题分类的相关文档ID
		
		return metaDataid;
	}
	
	/**
	 *行政审批按部门数据分发至部门下 TODO
	 */
	public String saveMetaViewDataByOrg(String XzspMetaDataId,XZSP xzsp){
		String metaDataId = "";
		String sServiceId = "wcm6_MetaDataCenter";//服务名
		String sMethodName = "saveMetaViewData";//方法名
		Map<String,Object> oPostData = new HashMap<String,Object>();
		//TODO 获取行政审批部门栏目ID
		
		oPostData.put("ObjectId", new Integer(0));
		oPostData.put("title", xzsp.getSxmc());
		oPostData.put("xgwd",XzspMetaDataId);//相关文档
		oPostData.put("ORG_CODE",xzsp.getORG_CODE());
		oPostData.put("itemId",xzsp.getItemId());
		oPostData.put("kbl","1");
		oPostData.put("xjpf",4);//星级评分
		return metaDataId;
	}
	
	
}
